var request = require('request');
var fs = require('fs');
var express = require('express');
const app = express()

var headers = {
    'Cache-Control': 'no-cache',
    'Content-Type': 'application/json'
};

var dataString = '{\n  "url": "https://www.baidu.com/",\n  "options": {\n    "fullPage": true,\n    "type": "jpeg",\n    "quality": 75\n  }\n}';

var options = {
    url: 'https://chrome.browserless.io/screenshot?token=a9121985-1e94-4569-bf5a-78eddb3a1ed9',
    method: 'POST',
    headers: headers,
    body: dataString
};

function callback(error, response, body) {
    console.log('==== respons ===');
    if (!error && response.statusCode == 200) {
        console.log(body);
        this.pipe(res)
    }
}
app.get('/api/download', (req, res, next) => {
    request(options).on('response', function(response) {
        console.log(response.statusCode) // 200
        console.log(response.headers)
        // console.log(response.headers['content-type']) // 'image/png'
        // res.headers['content-type'] = response.headers['content-type']
        this.pipe(res)
      });
});


app.listen(3000,()=>{
    console.log('serve is starting');
})


